#!/usr/bin/env python2.5

## Extracts the nominal phrases (not NP's proper, since we don't care about adjectives, determiners,...) from a sentence.

## Usage:  ~           (again, meant to be used modularly)

def NPs(words, tags, trace = False):
	"""Gets the NP's from a tagged sentence."""
	if (len(words) != len(tags)):
		print "Tagging error.  There are different numbers of tokens than there are tags."
		return list()
	nps = dict()
	x = 0
	if trace:	
		print words, tags, len(words), len(tags)
	while (x < len(tags)):
		coll = ""
		if (tags[x] in ["NN", "NNS", "NNP", "NNPS", "FW"]):
			while (tags[x] in ["NN", "NNS", "NNP", "NNPS", "FW"]):
				if trace:	print words[x], tags[x]
				if trace:	print x
				coll += words[x] + " "
				x += 1
				if (x == len(tags)):	break
		else:	x += 1
		if (coll):	nps[x - 1] = coll[:-1]
	return nps

if (__name__ == "__main__"):
	lexes = ["Meet", "the", "jade", "monkey", "beneath", "the", "willow", "trees", "of", "the", "Gobe", "Desert", "at", "noon", "."]
	poss = ["VB", "DT", "NN", "NN", "IN", "DT", "NNS", "NNS", "IN", "DT", "FW", "NNP", "IN", "NN", "DOT"]
	np = NPs(lexes, poss)
	for k in np.keys():
		print "Noun phrase:  " + np[k] + " ending at position", k
